package cn.stylefeng.guns.modular.goodstree.service.impl;

import cn.stylefeng.guns.base.pojo.node.TreeviewNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.goodstree.entity.LiuTree;
import cn.stylefeng.guns.modular.goodstree.mapper.LiuTreeMapper;
import cn.stylefeng.guns.modular.goodstree.model.params.LiuTreeParam;
import cn.stylefeng.guns.modular.goodstree.model.result.LiuTreeResult;
import cn.stylefeng.guns.modular.goodstree.service.LiuTreeService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品树表 服务实现类
 * </p>
 *
 * @author lcl
 * @since 2019-07-15
 */
@Service
public class LiuTreeServiceImpl extends ServiceImpl<LiuTreeMapper, LiuTree> implements LiuTreeService {

    @Resource
    private LiuTreeMapper liuTreeMapper;


    @Override
    public void add(LiuTree liuTree){
        if (ToolUtil.isOneEmpty(liuTree, liuTree.getSimpleName(), liuTree.getFullName(), liuTree.getPid())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //完善pids,根据pid拿到pid的pids
        this.deptSetPids(liuTree);

        this.save(liuTree);

    }

    @Override
    public void delete(Long brandId){
        this.liuTreeMapper.delete(brandId);
    }

    @Override
    public void update(LiuTree liuTree){
        if (ToolUtil.isOneEmpty(liuTree,  liuTree.getSimpleName(), liuTree.getFullName(), liuTree.getPid())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //完善pids,根据pid拿到pid的pids
        this.deptSetPids(liuTree);

        this.updateById(liuTree);

    }

    @Override
    public LiuTreeResult findBySpec(LiuTreeParam param){
        return null;
    }

    @Override
    public List<LiuTreeResult> findListBySpec(LiuTreeParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(LiuTreeParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public Page<Map<String, Object>> list(String condition) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page, condition);
    }

    private Serializable getKey(LiuTreeParam param){
        return param.getBrandId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private LiuTree getOldEntity(LiuTreeParam param) {
        return this.getById(getKey(param));
    }

    private LiuTree getEntity(LiuTreeParam param) {
        LiuTree entity = new LiuTree();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    @Override
    public List<TreeviewNode> treeviewNodes() {
        return this.baseMapper.treeviewNodes();
    }


    /**
     * 设置部门的父级ids
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:58 PM
     */
    private void deptSetPids(LiuTree liuTree) {
        if (ToolUtil.isEmpty(liuTree.getPid()) || liuTree.getPid().equals(0L)) {
            liuTree.setPid(0L);
            liuTree.setPids("[0],");
        } else {
            Long pid = liuTree.getPid();
            LiuTree temp = this.getById(pid);
            String pids = temp.getPids();
            liuTree.setPid(pid);
            liuTree.setPids(pids + "[" + pid + "],");
        }
    }

}
